package com.hserver.beetlsql.service;

import com.hserver.beetlsql.bean.User;
import com.hserver.beetlsql.dao.UserDao;
import org.beetl.sql.core.DSTransactionManager;
import org.beetl.sql.core.SQLManager;
import top.hserver.core.ioc.annotation.Autowired;
import top.hserver.core.ioc.annotation.Bean;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Bean
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private SQLManager sqlManager;

    /**
     * 事务测试
     *
     * @return
     */
    public boolean update() {
        DSTransactionManager.start();
        userDao.update(21, 1);
        userDao.update(22, 1);
        userDao.update(20, 1);
        try {
            DSTransactionManager.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DSTransactionManager.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return false;
    }

    /**
     * sqlmanager 默认在容器中，可以直接进行获取然后使用
     *
     * @return
     */
    public boolean add() {
        User user = new User();
        user.setName("777");
        user.setAge(20);
        user.setCreateDate(new Date());
        return sqlManager.insert(user) > 0;
    }

    /**
     * Mapper你的dao导入使用
     *
     * @return
     */
    public List<User> select() {
        return userDao.select();
    }

}
